import { getUserInfo } from '@/api/user';
import type { User } from '@/types';
import { defineStore } from 'pinia';
interface UserState {
  totalBook: number;
  token: string;
  userInfo: User;
}

export const useUserStore = defineStore('user', {
  state: (): UserState => ({
    totalBook: 0,
    token: '',
    userInfo: {
      id: '',
      avatar: '',
      intro: '',
      nickName: '',
      phone: 0,
      password: '',
      status: 0,
      updateTime: 0,
      createTime: 0,
      province: '',
      city: '',
      number: '',
      checkNickName: '',
      checkIntro: '',
      checkAvatar: '',
      checkStatus: 0,
    },
  }),
  getters: {
    isLoggedIn: (state) => Boolean(state.token),
  },
  actions: {
    getUserInfo() {
      getUserInfo().then((res) => {
        this.userInfo = res.result;
      });
    },
    logout() {
      this.token = '';
      localStorage.removeItem('kuaikuai');
    },
  },
});
